package dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import pojo.Address;
import pojo.SysUser;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SysUserMapprTest<Logger> {
Logger logger = Logger.getLogger(SysUserMapprTest.class);
    private Object MyBatisUtil;
//    @Test
//    public void count() throws IOException {
//
//        //读取MyBatis框架的核心配置文件
//
//        String resource = "mybatis-config.xml";
//
//        //获取对mybatis-config.xml的输入流
//
//        InputStream is = Resources.getResourceAsStream(resource);
//
//        //使用SqlSessionFactoryBuilder读取配置文件并构架
//        //SqlSessionFactory实列
//
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//
//        //创建SqlSession实列
//
//        SqlSession sqlSession = factory.openSession();
//
//        //创建sysUserMapper接口实例，调用其方法执行相关的SQl语句
//
//        int count = sqlSession.getMapper(SysUserMappr.class).count();
//        NoLoggingImpl logger = null;
//        logger.debug("SysUserMapperTest count ---" + count);
//
//        //5.关闭 SqlSession
//
//        sqlSession.close();
//    }

//    @Test
//    public void getUserList() throws IOException {
//        SqlSession sqlSession = null;
//        List<SysUser> userList = null;
//
//        try {
//            //读取MyBatis框架的核心配置文件
//
//            String resource = "mybatis-config.xml";
//
//            //获取对mybatis-config.xml的输入流
//
//            InputStream is = Resources.getResourceAsStream(resource);
//
//            //使用SqlSessionFactoryBuilder读取配置文件并构建
//            //sqlSessionFactory实例
//
//            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//
//            //创建SqlSession实例
//
//            sqlSession = factory.openSession();
//
//            //创建SysUserMapper接口实例，调用其方法执行相关的sql语句
//
//            userList = sqlSession.getMapper(SysUserMappr.class).getUserList();
//        } finally {
//            //关闭SqlSession
//
//            sqlSession.close();
//        }
//        if (userList != null) {
//            for (SysUser user : userList) {
//                NoLoggingImpl logger = null;
//                logger.debug(
//                        "testUserListQuery account:" + user.getAccount()
//                                + "and realName:" + user.getRealName());
//            }
//        }
//    }
    @Test
    public void count() throws IOException {

        //读取MyBatis框架的核心配置文件

        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);

        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();

        //创建sysUserMapper接口实例，调用其方法执行相关的SQl语句

        int count = sqlSession.getMapper(SysUserMappr.class).count();
        logger.debug("SysUserMapperTest count ---" + count);

        //5.关闭 SqlSession

        sqlSession.close();
    }

    @Test
    public void getUserList() throws IOException {
        SqlSession sqlSession = null;
        List<SysUser> userList = null;

        try {
            //读取MyBatis框架的核心配置文件

            String resource = "mybatis-config.xml";

            //获取对mybatis-config.xml的输入流

            InputStream is = Resources.getResourceAsStream(resource);

            //使用SqlSessionFactoryBuilder读取配置文件并构建
            //sqlSessionFactory实例

            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

            //创建SqlSession实例

            sqlSession = factory.openSession();

            //创建SysUserMapper接口实例，调用其方法执行相关的sql语句

            userList = sqlSession.getMapper(SysUserMappr.class).getUserList();
        } finally {
            //关闭SqlSession

            sqlSession.close();
        }
        if (userList != null) {
            for (SysUser user : userList) {
                logger.debug(
                        "testUserListQuery account:" + user.getAccount()
                                + "and realName:" + user.getRealName());
            }
        }
    }

    @Test
    public void  getUsersByRealName(String realName) throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        List<SysUser>userList = sqlSession.getMapper(SysUserMappr.class).getUsersByRealName("李");
        sqlSession.close();
    }

    @Test
    public void getUserListByPojo(SysUser sysUser) throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";
        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        SysUser user = new SysUser();
        user.setRealName("李");
        user.setRoleId(3);
        List<SysUser> userList = sqlSession.getMapper(SysUserMappr.class).getUserListByPojo(user);
        sqlSession.close();
    }

    @Test
    public void getUserListByMap() throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        Map<String, Object> userMap = new HashMap<String, Object>();
        userMap.put("rName","赵");
        userMap.put("rId",2);
        List<SysUser>userList = sqlSession.getMapper(SysUserMappr.class).getUserListByMap(userMap);
        sqlSession.close();
    }

    @Test
    public void getUserListByParams() throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        List<SysUser>userList = sqlSession.getMapper(SysUserMappr.class).getUserListParams("赵","2");
        sqlSession.close();
    }

    @Test
    public void getUserListWithRoleName() throws  Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        SysUser user = new SysUser();
        user.setRealName("李");
        user.setRoleId(3);
        sqlSession.close();
    }

    @Test
    public void getUserListByRoleId() throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        List<SysUser> userList = sqlSession.getMapper(SysUserMappr.class).getUserListByRoleId(2);
        sqlSession.close();
    }

    @Test
    public void getUserAndAddressesByUserId() throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        List<SysUser>userList= sqlSession.getMapper(SysUserMappr.class).getUserAndAddressesByUserId(1);
        sqlSession.close();
        for (SysUser user:userList){
            logger.debug("userList(include:address's) =====> account"+user.getAccount()+",realName:"+user.getRealName());
            for (Address address : user.getAddresses()){
                logger.debug("address -----> id:"+address.getId()+",contact:"
                +address.getContact()+",addressDesc:"+address.getAddressDesc()
                +",tel:"+address.getTel()
                +",postCode:" + address.getPostCode()
                );
            }
        }
    }

    @Test
    public void testAdd() throws IOException {

        logger.debug("testAdd !==================");
        SqlSession sqlSession1 = null;
        int count = 0;
        try {
            //读取MyBatis框架的核心配置文件
            String resource = "mybatis-config.xml";

            //获取对mybatis-config.xml的输入流

            InputStream is = Resources.getResourceAsStream(resource);
            //使用SqlSessionFactoryBuilder读取配置文件并构架
            //SqlSessionFactory实列

            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

            //创建SqlSession实列

            SqlSession sqlSession = factory.openSession();
            SysUser user= new SysUser();
            user.setAccount("test001");
            user.setRealName("测试用户001");
            user.setPassword("1234567");
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("2003-10-22");
            user.setBirthday(birthday);
            user.setAddress("测试地址abc");
            user.setSex(1);
            user.setPhone("1341110000");
            user.setRoleId(1);
            user.setCreatedUserId(1);
            user.setCreatedTime(new Date());
            count = sqlSession.getMapper(SysUserMappr.class).add(user);
            sqlSession.close();
        } catch (ParseException e) {
            e.printStackTrace();
            sqlSession1.rollback();
            count=0;
        }
        logger.debug("testAdd count:"+ count );
    }

    @Test
    public void testUpdatePwd() throws IOException {
        logger.debug("testUpdatePwd !===============");
        String pwd = "8888888";
        Integer id = 17;
        int count = 0;
        try {
            //读取MyBatis框架的核心配置文件
            String resource = "mybatis-config.xml";

            //获取对mybatis-config.xml的输入流

            InputStream is = Resources.getResourceAsStream(resource);
            //使用SqlSessionFactoryBuilder读取配置文件并构架
            //SqlSessionFactory实列

            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

            //创建SqlSession实列

            SqlSession sqlSession = factory.openSession();
            count = sqlSession.getMapper(SysUserMappr.class).updatePwd(id,pwd);
            sqlSession.close();
        }catch (Exception e){
            e.printStackTrace();
            count=0;
        }
        logger.debug("testUpdatePwd count:"+count);
    }

    @Test
    public void selectList() throws Exception{
        //读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";

        //获取对mybatis-config.xml的输入流

        InputStream is = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder读取配置文件并构架
        //SqlSessionFactory实列

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //创建SqlSession实列

        SqlSession sqlSession = factory.openSession();
        String realName = "赵";
        Integer roleId = 2;
        List<SysUser> userList = sqlSession.getMapper(SysUserMappr.class).selectList(realName, roleId);
        sqlSession.close();
    }

    public void testGetUserListByChoose() throws Exception{
        List<SysUser> userList = new ArrayList<SysUser>();
        try {
            //读取MyBatis框架的核心配置文件
            String resource = "mybatis-config.xml";

            //获取对mybatis-config.xml的输入流

            InputStream is = Resources.getResourceAsStream(resource);
            //使用SqlSessionFactoryBuilder读取配置文件并构架
            //SqlSessionFactory实列

            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

            //创建SqlSession实列

            SqlSession sqlSession = factory.openSession();
            String realName = "";
            Integer roleId = 2;
            String account = "`zhong`";
            Date createdTime = new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-11");
            userList = sqlSession.getMapper(SysUserMappr.class).selectList(realName,roleId,account,createdTime);
            sqlSession.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("查询到用户数量："+userList.size());
        for (SysUser user: userList){
            logger.info("查询到用户信息："+user);
        }
    }
}